package com.ruoyi.plus.web.system;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.util.ShiroUtils;
import com.ruoyi.plus.domain.SysTenant;
import com.ruoyi.plus.service.ISysTenantService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 租户管理Controller
 *
 * @author likun
 * @date 2020-01-13
 */
@Controller
@RequestMapping("/system/tenant")
public class SysTenantController extends BaseController {
    private String prefix = "system/tenant";

    @Autowired
    private ISysTenantService sysTenantService;

    @RequiresPermissions("system:tenant:view")
    @GetMapping()
    public String tenant() {
        return prefix + "/tenant";
    }

    /**
     * 查询租户管理列表
     */
    @RequiresPermissions("system:tenant:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(SysTenant sysTenant) {
        startPage();
        List<SysTenant> list = sysTenantService.selectSysTenantList(sysTenant);
        return getDataTable(list);
    }

    /**
     * 导出租户管理列表
     */
    @RequiresPermissions("system:tenant:export")
    @Log(title = "租户管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(SysTenant sysTenant) {
        List<SysTenant> list = sysTenantService.selectSysTenantList(sysTenant);
        ExcelUtil<SysTenant> util = new ExcelUtil<SysTenant>(SysTenant.class);
        return util.exportExcel(list, "tenant");
    }

    /**
     * 新增租户管理
     */
    @GetMapping("/add")
    public String add() {
        return prefix + "/add";
    }

    /**
     * 新增保存租户管理
     */
    @RequiresPermissions("system:tenant:add")
    @Log(title = "租户管理", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(SysTenant sysTenant) {
        return toAjax(sysTenantService.insertSysTenant(sysTenant));
    }

    /**
     * 修改租户管理
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap) {
        SysTenant sysTenant = sysTenantService.selectSysTenantById(id);
        mmap.put("sysTenant", sysTenant);
        return prefix + "/edit";
    }

    /**
     * 修改保存租户管理
     */
    @RequiresPermissions("system:tenant:edit")
    @Log(title = "租户管理", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(SysTenant sysTenant) {
        sysTenant.setUpdateBy(ShiroUtils.getLoginName());
        return toAjax(sysTenantService.updateSysTenant(sysTenant));
    }

    /**
     * 删除租户管理
     */
    @RequiresPermissions("system:tenant:remove")
    @Log(title = "租户管理", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(sysTenantService.deleteSysTenantByIds(ids));
    }

    /**
     * 校验登录账号
     */
    @PostMapping("/checkLoginNameUnique")
    @ResponseBody
    public String checkLoginNameUnique(SysTenant tenant) {
        return sysTenantService.checkLoginNameUnique(tenant.getLoginName());
    }

    /**
     * 校验手机号码
     */
    @PostMapping("/checkMobileUnique")
    @ResponseBody
    public String checkMobileUnique(SysTenant tenant) {
        return sysTenantService.checkMobileUnique(tenant);
    }

    /**
     * 校验email邮箱
     */
    @PostMapping("/checkEmailUnique")
    @ResponseBody
    public String checkEmailUnique(SysTenant tenant) {
        return sysTenantService.checkEmailUnique(tenant);
    }

    @RequiresPermissions("system:tenant:resetPwd")
    @GetMapping("/resetPwd/{id}")
    public String resetPwd(@PathVariable("id") Long id, ModelMap mmap) {
        mmap.put("sysTenant", sysTenantService.selectSysTenantById(id));
        return prefix + "/resetPwd";
    }

    @RequiresPermissions("system:tenant:resetPwd")
    @Log(title = "重置密码", businessType = BusinessType.UPDATE)
    @PostMapping("/resetPwd")
    @ResponseBody
    public AjaxResult resetPwdSave(SysTenant tenant) {
        if (sysTenantService.resetUserPwd(tenant) > 0) {
            return success();
        }
        return error();
    }

    @RequiresPermissions("system:tenant:grantAuth")
    @GetMapping("/grantAuth/{id}")
    public String grantAuth(@PathVariable("id") Long id, ModelMap mmap) {
        mmap.put("sysTenant", sysTenantService.selectSysTenantById(id));
        return prefix + "/grantAuth";
    }

    @RequiresPermissions("system:tenant:grantAuth")
    @Log(title = "租户授权", businessType = BusinessType.UPDATE)
    @PostMapping("/grantAuth")
    @ResponseBody
    public AjaxResult grantAuthSave(SysTenant sysTenant,Long[] menuIds) {
        if (sysTenantService.grantAuthSave(sysTenant,menuIds) > 0){
            return success();
        }
        return error();
    }


}
